package com.example.hemin.fnb.ui.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.hemin.fnb.ui.activity.MainActivity;
import com.example.hemin.fnb.ui.activity.PasswordActivity;
import com.example.hemin.fnb.ui.base.BasePresenter;
import com.example.hemin.fnb.ui.bean.BaseObjectBean;
import com.example.hemin.fnb.ui.bean.UserDateBean;
import com.example.hemin.fnb.ui.contract.PwLoginContract;
import com.example.hemin.fnb.ui.fragment.TabMyFragment;
import com.example.hemin.fnb.ui.model.PasswordModel;
import com.example.hemin.fnb.ui.net.RxScheduler;
import com.example.hemin.fnb.ui.util.MyConsumer;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;

import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class PasswordPresenter extends BasePresenter<PasswordActivity> implements PwLoginContract.Presenter {
    private PwLoginContract.Model model;

    public PasswordPresenter() {
        model = new PasswordModel();
    }

    @Override
    public void pwLogn(final Context context, RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        Gson gson = new Gson();
        String data = "      \n" +
                "{\n" +
                "    \"Authorization\":\"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzA4MTk2Mjk2OSIsImV4cCI6MzM1ODY4ODEzOH0.aKDkU-hosX7I2G6Lyby20-sPa6vqop4ARhcEiZDy3lWVqD6jzneMYpWYBRFUzrGKGzViY6uLX-uMfVh0VWGZyg\",\n" +
                "    \"token_type\":\"usERa\",\n" +
                "    \"expires_in\":1800000000,\n" +
                "    \"user\":{\n" +
                "        \"userId\":9,\n" +
                "        \"nickname\":\"130****2969\",\n" +
                "        \"url\":\"https://funwl.oss-cn-hangzhou.aliyuncs.com/images/u%3D1229021758%2C2220622610%26fm%3D27%26gp%3D0.jpg\",\n" +
                "        \"mobile\":\"130****2969\",\n" +
                "        \"birthday\":\"\",\n" +
                "        \"signature\":\"\",\n" +
                "        \"sex\":\"男\"\n" +
                "    }\n" +
                "}";
        UserDateBean.DataBean bean = gson.fromJson(data,new TypeToken<UserDateBean.DataBean>(){}.getType());
        String Authorization = bean.getAuthorization();
        String tokenType = bean.getToken_type();
        String expiresIn = bean.getExpires_in();
        UserDateBean.DataBean.UserBean user = bean.getUser();
        String userId = user.getUserId();
        String nickName = user.getNickname();
        String url = user.getUrl();
        String mobile = user.getMobile();
        String birthday = user.getBirthday();
        String signature = user.getSignature();
        String sex = user.getSex();
        SharedPreferences.Editor editor = context.getSharedPreferences("userDate", Context.MODE_PRIVATE).edit();
        editor.putString("Authorization", Authorization);
        editor.putString("tokenType", tokenType);
        editor.putString("expiresIn", expiresIn);
        editor.putString("userId", userId);
        editor.putString("nickName", nickName);
        editor.putString("url", url);
        editor.putString("mobile", mobile);
        editor.putString("birthday", birthday);
        editor.putString("signature", signature);
        editor.putString("sex", sex);
        editor.putBoolean("key", false);
        editor.commit();
        Intent intent = new Intent(context, MainActivity.class);
                           context.startActivity(intent);
        mView.hideLoading();
//        model.pwLogin(context, body)
//                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
//                .as(mView.<BaseObjectBean>bindAutoDispose())
//                .subscribe(new Consumer<BaseObjectBean>() {
//
//                    @Override
//                    public void accept(BaseObjectBean beans) throws Exception {
//                        mView.onSuccess(beans);
//                        mView.hideLoading();
////                 Gson gson = new Gson();
////                 String data = "{\"Authorization\":\"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzA4MTk2Mjk2OSIsImV4cCI6MzM1ODY4ODEzOH0.aKDkU-hosX7I2G6Lyby20-sPa6vqop4ARhcEiZDy3lWVqD6jzneMYpWYBRFUzrGKGzViY6uLX-uMfVh0VWGZyg\",\"token_type\":\"usERa\",\"expires_in\":1800000000,\"user\":{\"userId\":9,\"nickname\":\"130****2969\",\"url\":\"https://funwl.oss-cn-hangzhou.aliyuncs.com/images/u%3D1229021758%2C2220622610%26fm%3D27%26gp%3D0.jpg\",\"mobile\":\"130****2969\",\"birthday\":\"\",\"signature\":\"\",\"sex\":\"男\"}}";
////                        UserDateBean.DataBean bean = gson.fromJson(data,new TypeToken<UserDateBean>(){}.getType());
//////                        UserDateBean.DataBean bean = (UserDateBean.DataBean) beans.getResult();
////                           String Authorization = bean.getAuthorization();
////                            String tokenType = bean.getToken_type();
////                            String expiresIn = bean.getExpires_in();
////                            UserDateBean.DataBean.UserBean user = bean.getUser();
////                            String userId = user.getUserId();
////                            String nickName = user.getNickname();
////                            String url = user.getUrl();
////                            String mobile = user.getMobile();
////                            String birthday = user.getBirthday();
////                            String signature = user.getSignature();
////                            String sex = user.getSex();
////                            SharedPreferences.Editor editor = context.getSharedPreferences("userDate", Context.MODE_PRIVATE).edit();
////                            editor.putString("Authorization", Authorization);
////                            editor.putString("tokenType", tokenType);
////                            editor.putString("expiresIn", expiresIn);
////                            editor.putString("userId", userId);
////                            editor.putString("nickName", nickName);
////                            editor.putString("url", url);
////                            editor.putString("mobile", mobile);
////                            editor.putString("birthday", birthday);
////                            editor.putString("signature", signature);
////                            editor.putString("sex", sex);
////                            editor.putBoolean("key", false);
////                            editor.commit();
//
//
//                    }
//
////                    @Override
////                    public void accept(UserDateBean bean, boolean success, BaseObjectBean baseObjectBean) {
////                        // code == 0 bean 是重新处理以后的数据
////
////                        if(baseObjectBean.getErrorCode() == 0){
////                            String Authorization = bean.getAuthorization();
////                            String tokenType = bean.getToken_type();
////                            String expiresIn = bean.getExpires_in();
////                            UserDateBean.User user = bean.getUser();
////                            String userId = user.getUserId();
////                            String nickName = user.getNickname();
////                            String url = user.getUrl();
////                            String mobile = user.getMobile();
////                            String birthday = user.getBirthday();
////                            String signature = user.getSignature();
////                            String sex = user.getSex();
////                            SharedPreferences.Editor editor = context.getSharedPreferences("userDate", Context.MODE_PRIVATE).edit();
////                            editor.putString("Authorization", Authorization);
////                            editor.putString("tokenType", tokenType);
////                            editor.putString("expiresIn", expiresIn);
////                            editor.putString("userId", userId);
////                            editor.putString("nickName", nickName);
////                            editor.putString("url", url);
////                            editor.putString("mobile", mobile);
////                            editor.putString("birthday", birthday);
////                            editor.putString("signature", signature);
////                            editor.putString("sex", sex);
////                            editor.putBoolean("key", false);
////                            editor.commit();
////                            Intent intent = new Intent(context, MainActivity.class);
////                            context.startActivity(intent);
////                        }
//////                        UserDateBean result = gson.fromJson(jsonObject.getJSONObject("data").toString(), UserDateBean.class);
////
////                    }
//
////                    @Override
////                    public void accept_error(String bean, boolean success, String message) {
////                        //不是 code == 0 失败
////                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        mView.onError(throwable);
//                        mView.hideLoading();
//                    }
//                });
    }
}
